﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using iPhOwa.Exchange.Managers;
using System.Text;
using iPhOwa.Exchange.ContentClasses;

namespace iPhOwa.Web
{
	/// <summary>
	/// Compose message.
	/// </summary>
	public partial class ComposeMessage : iPhOwaPage
	{
		/// <summary>
		/// Page load event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				string action = Request["action"] ?? "new";

				switch (action.ToLowerInvariant())
				{
					case "reply":
						{
							Message message = MessageManager.Get(Path, OwaSession);

							if (message == null)
							{
								throw new ApplicationException("Expected message, but got something else.");
							}

							toTextBox.Text = message.From;
							subjectTextBox.Text = "RE: " + message.NormalizedSubject;
							bodyTextBox.Text = "\n\n\nOriginal Message:\n\n" + message.TextDescription.Trim();

							break;
						}

					case "replyall":
						{
							Message message = MessageManager.Get(Path, OwaSession);

							if (message == null)
							{
								throw new ApplicationException("Expected message, but got something else.");
							}

							toTextBox.Text = message.From;
							subjectTextBox.Text = "RE: " + message.NormalizedSubject;
							bodyTextBox.Text = "\n\n\nOriginal Message:\n\n" + message.TextDescription.Trim();

							toTextBox.Text += "; " + message.To;

							if (!string.IsNullOrEmpty(message.CC))
							{
								ccRow.Visible = true;
								ccTextBox.Text = message.CC;
							}

							break;
						}

					case "forward":
						{
							Message message = MessageManager.Get(Path, OwaSession);

							if (message == null)
							{
								throw new ApplicationException("Expected message, but got something else.");
							}

							subjectTextBox.Text = "FW: " + message.NormalizedSubject;
							bodyTextBox.Text = "\n\n\nOriginal Message:\n\n" + message.TextDescription.Trim();

							break;
						}

					case "new":
					default:

						break;
				}
			}
		}

		/// <summary>
		/// Cancel button click event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void CancelButton_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/Default.aspx");
		}

		/// <summary>
		/// Send button click event.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void SendButton_Click(object sender, EventArgs e)
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("To: {0}\n", toTextBox.Text.Trim());
			sb.AppendFormat("Subject: {0}\n", subjectTextBox.Text.Trim());
			sb.AppendFormat("Date: {0}\n", DateTime.Now);
			sb.Append("X-Mailer: iPhOwa\n");
			sb.Append("MIME-Version: 1.0\n");
			sb.Append("Content-Type: text/plain;\n");
			sb.Append("Charset = \"iso-8859-1\"\n");
			sb.Append("Content-Transfer-Encoding: 7bit\n\n");
			sb.Append(bodyTextBox.Text.Trim());

			MessageManager.Send(MailSubmissionUrl, sb.ToString(), OwaSession);

			Response.Redirect("~/Default.aspx");
		}
	}
}
